package com.alisls.shequtao.service.goods.web;

import com.alisls.shequtao.common.model.result.PageResult;
import com.alisls.shequtao.service.goods.dto.SpuDTO;
import com.alisls.shequtao.service.goods.dto.SpuDetailDTO;
import com.alisls.shequtao.service.goods.query.SpuPageQuery;
import com.alisls.shequtao.service.goods.service.SpuDetailService;
import com.alisls.shequtao.service.goods.service.SpuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Spu管理
 *
 * @author Ke Wang
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("/spu")
@AllArgsConstructor
public class SpuController {

    private final SpuService spuService;

    private final SpuDetailService spuDetailService;

    /**
     * 根据商品标识查询商品
     */
    @ApiOperation(value = "商品查询", notes = "根据商品标识查询商品")
    @ApiImplicitParam(
            name = "id",
            required = true,
            paramType = "path",
            dataType = "Long",
            example = "1234567890"
    )
    @RequestMapping("/getSpuById/{id}")
    public ResponseEntity<SpuDTO> getSpuById(@PathVariable Long id) {
        SpuDTO spuDTO = spuService.getSpuById(id);
        return ResponseEntity.ok(spuDTO);
    }

    @ApiOperation(value = "商品详情查询", notes = "根据商品标识查询商品详情")
    @RequestMapping("/getSpuDetailById/{id}")
    public ResponseEntity<SpuDetailDTO> getSpuDetailById(@PathVariable Long id) {
        SpuDetailDTO spuDetailDTO = spuDetailService.convertToDTO(spuDetailService.getById(id));
        return ResponseEntity.ok(spuDetailDTO);
    }

    @ApiOperation(value = "分页查询商品")
    @GetMapping("/getSpuByPage")
    public ResponseEntity<PageResult<SpuDTO>> getSpuByPage(@Valid SpuPageQuery spuPageQuery) {
        PageResult<SpuDTO> spuDTOPageResult = spuService.getSpuByPage(spuPageQuery);
        return ResponseEntity.ok(spuDTOPageResult);
    }

}
